package com.shinhye.wisdompeking.pager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.shinhye.wisdompeking.ConstantParams;
import com.shinhye.wisdompeking.R;
import com.shinhye.wisdompeking.base.BasePager;
import com.shinhye.wisdompeking.bean.NewsCenter;
import com.shinhye.wisdompeking.bean.NewsCenter.NewCenterItem;
import com.shinhye.wisdompeking.utils.SharedUtils;

public class NewsCenterPager extends BasePager {
	private static final String TAG = "NewsCenterPager";
	private FrameLayout news_center_fl;
	private Map<Integer, BasePager> newscenter_pagers = new HashMap<Integer, BasePager>();;
	private List<String> list_menu = new ArrayList<String>();
	private NewsCenter newCenter;

	public NewsCenterPager(Context context) {
		super(context);
	}

	@Override
	public View initView() {
		view = View.inflate(context, R.layout.news_center_frame, null);
		
		news_center_fl = (FrameLayout) view.findViewById(R.id.news_center_fl);
		initTitleBar();
		return view;
	}

	@Override
	public void initData() {
		//从本地获取数据
		String center_result = SharedUtils.getString(context, ConstantParams.NEWS_CENTER_CATEGORIES, "");
		if(TextUtils.isEmpty(center_result)) {
			obtainNetData();
		} else {
			Log.i(TAG, "本地获取到数据了");
			setData(center_result);
		}
	}

	//从网络获取数据
	private void obtainNetData() {
		Log.i(TAG, "开始获取网络数据: " + ConstantParams.NEWS_CENTER_CATEGORIES);
		
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.POST, ConstantParams.NEWS_CENTER_CATEGORIES, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Log.i(TAG, responseInfo.result);
				
				SharedUtils.putString(context, ConstantParams.NEWS_CENTER_CATEGORIES, responseInfo.result);
				setData(responseInfo.result);
			}
			@Override
			public void onFailure(HttpException error, String msg) {}
		});
	}
	//设置获取的数据到界面
	private void setData(String result) {
		//GSON
		Gson gson = new Gson();
		newCenter = gson.fromJson(result, NewsCenter.class);
		Log.i(TAG, "oooooooooooo = " + newCenter.data.get(0).children.get(0).url);
		
		List<NewCenterItem> list = newCenter.data;
		for (NewCenterItem newCenterItem : list) {
			list_menu.add(newCenterItem.title);
		}
		getActivity().getMenuFragment().initMenu(list_menu);
		
		switchNewsCenterPager(0);
	}
	
	public void switchNewsCenterPager(int position) {
		getTitle().setText(list_menu.get(position));
		
		BasePager pager = newscenter_pagers.get(position);
		if(pager == null) {
			switch (position) {
			case 0:
				Log.i(TAG, "创建了新闻页");
				pager = new NewsPager(context, newCenter.data.get(0));
				break;
			case 1:
				Log.i(TAG, "创建了专题页");
				pager = new SubjectPager(context, newCenter.data.get(1));
				break;
			case 2:
				Log.i(TAG, "创建了组图页");
				pager = new PicturesPager(context, newCenter.data.get(2));
				break;
			case 3:
				Log.i(TAG, "创建了互动页");
				pager = new InteractPager(context, newCenter.data.get(3));
				break;
			}
			newscenter_pagers.put(position, pager);
		}
		
		news_center_fl.removeAllViews();
		news_center_fl.addView(pager.getRootView());
		
		pager.initData();
	}
}
